//
//  ViewController.m
//  HomeWorkAddressBook
//
//  Created by 苗钧豪 on 16/8/8.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "ViewController.h"
#import "TableViewController.h"
#import "SQLiteDataBaseManager.h"
#import <AssetsLibrary/AssetsLibrary.h>
#define IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ? YES : NO)
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)SQLiteDataBaseManager *DataBaseManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    }
- (IBAction)photo:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"获取图片" message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"进入相册" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePickerController animated:YES completion:nil];
         }];
    UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:alertAction1];
    [alertController addAction:alertAction2];
    [self presentViewController:alertController animated:YES completion:nil];
    

}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    _img = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSData *data = UIImagePNGRepresentation(_img);
    NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *document = [pathArr firstObject];
    _imgPath = [document stringByAppendingPathComponent:@"picture"];
    [data writeToFile:_imgPath atomically:YES];
    
}
- (IBAction)save:(id)sender {
    [_DataBaseManager openSQLite];
    _stu.stu_name = [NSString stringWithFormat:@"%@",_nameF.text];
    _stu.stu_gender = [NSString stringWithFormat:@"%@",_genderF.text];
    _stu.stu_number = [NSString stringWithFormat:@"%@",_numberF.text];
    _imgview.image = _img;
    _stu.stu_image = _imgPath;
    TableViewController *vc = self.navigationController.viewControllers.firstObject;
    [_DataBaseManager insertStu:_stu];
    [vc.Arr addObject:_stu];
    [_DataBaseManager closeSQLite];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
